//生成菜单
var menuItem = Vue.extend({
	name: 'menu-item',
	props:{item:{},index:0},
	template:[
	'<li :class="{active: (item.level===0 && index === 0)}">',
	'<a v-if="item.level === 0" href="javascript:;">',
        // '<i v-if="item.icon != null" :class="item.icon"></i>',
        '<span>{{item.name}}</span>',
        '<i class="fa fa-angle-left pull-right"></i>',
        '</a>',
        // '<ul v-if="item.level === 0" class="treeview-menu">',
        // '<menu-item :item="item" :index="index" v-for="(item, index) in item.list"></menu-item>',
        // '</ul>',
        '<a v-if="item.level === 1" :href="\'#\'+item.url">' +
        '<i v-if="item.icon != null" :class="item.icon"></i>' +
        '<i v-else class="fa fa-circle-o"></i> {{item.name}}' +
        '</a>',
        '</li>'
        ].join('')
    });

//iframe自适应
$(window).on('resize', function() {
	var $content = $('.content');
	$content.height($(this).height() - 120);
	$content.find('iframe').each(function() {
		$(this).height($content.height());
	});
}).resize();

//注册菜单组件
Vue.component('menuItem',menuItem);

var vm = new Vue({
	el:'#rrapp',
	data:{
		user:{},
		menuList:[],
		main:"",
		form:{
			password:'',
			newPassword:''
		},
		navTitle:"欢迎页"
	},
	methods: {
		websockeInfo: function () {
            var websocket  = new WebSocket("ws://127.0.0.1:10086/ontrial/echo");
            websocket.onerror = function(event) {
                alert('websockt连接发生错误，请刷新页面重试!')
            };
            // 连接成功建立的回调方法
            websocket.onopen = function() {
                console.log('websocket已连接-----------')
                websocket.send('用户已登录'); // 或者直接发送websocke.send(null)
            };
            // 接收到消息的回调方法
            websocket.onmessage = function(msg) {
				$("#noticeInfo").text("sfsffdf");
			}
        },
		getMenuList: function () {
			$.ajax({
				type: "GET",
				url: baseURL + "/menu/list",
				xhrFields: {
                	withCredentials: true // 这里设置了withCredentials
            	},
            	success: function(r){
            		vm.menuList = r.data;
            	},
            	error:function(){

            	}
        });

		},
		getUser: function(){
			$.getJSON(baseURL + "/auth/self", function(r){
				vm.user = r.data;
				if(r.data.roleCode=="0002"){
					vm.main="merchant/merchantmain.html";
				}else{
					vm.main="manager/main.html";
				}
			});
		},
		viewInfo: function() {
			if($(".badge").text() > 0){
                console.log("sfsdf")
                $("#viewInfoModel").modal('show');
			}
        },
		updatePassword: function(){
			layer.open({
				type: 1,
				skin: 'layui-layer-molv',
				title: "修改密码",
				area: ['550px', '280px'],
				shadeClose: false,
				contentType:"application/json",
				content: jQuery("#passwordLayer"),
				btn: ['修改','取消'],
				btn1: function (index) {
					$.ajax({
						type: "POST",
						url: baseURL + "/auth/password/reset",
						dataType: "json",
						contentType: "application/json",
						data: JSON.stringify(vm.form),
						success: function(r){
							if(r.code == 0){
								layer.close(index);
								layer.alert('修改成功', function(){
									location.reload();
								});
							}else{
								layer.alert(r.info);
							}
						}
					});
				}
			});
		},
		logout: function () {
			var data={"token":sessionStorage.getItem("token")};
			$.ajax({
				type: "POST",
				contentType:"application/json",
				url: baseURL + "/auth/signout",
				dataType: "json",
				data:JSON.stringify(data),
				success: function(r){
                    //删除本地token
                    sessionStorage.removeItem("token");
                    sessionStorage.removeItem("roleCode");
                    //跳转到登录页面
                    location.href = '/login.html';
                }
            });
		}
	},
	created: function(){
		this.websockeInfo();
		this.getMenuList();
		this.getUser();
	},
	updated: function(){
		//路由
		var router = new Router();
		routerList(router, vm.menuList);
		router.start();
	}
});



function routerList(router, menuList){
	for(var key in menuList){
		var menu = menuList[key];
		if(menu.level == 0){
			routerList(router, menu.list);
		}else if(menu.level == 1){
			router.add('#'+menu.url, function() {
				var url = window.location.hash;
				
				//替换iframe的url
				vm.main = url.replace('#', '');

			    //导航菜单展开
			    $(".treeview-menu li").removeClass("active");
			    $(".sidebar-menu li").removeClass("active");
			    $("a[href='"+url+"']").parents("li").addClass("active");
			    
			    vm.navTitle = $("a[href='"+url+"']").text();
			});
		}
	}
}
